<script setup>
import { addWarehouseApi, getWarehouseListApi, updateWarehouseApi } from '@/api/warehouse';
import { addWarehouseAreaApi, getWarehouseAreaListApi, updateWarehouseAreaApi } from '@/api/warehouseArea';
import { ElMessage } from 'element-plus';
import { getCurrentInstance, onMounted, ref } from 'vue';


//获取当前Vue实例
const instance = getCurrentInstance()
const ruleFormRef = ref(null)
const dialogFormVisible = ref(false)
const title = ref('')
const Warehouse = ref([])
const AreaForm = ref({
    id: '',
    name: '',
    warehouseId: '',
    category: '',
})
const areaType = ref([
    { label: '拣货区', value: '拣货区' },
    { label: '备货区', value: '备货区' },
    { label: '收货区', value: '收货区' },
    { label: '退货区', value: '退货区' },
    { label: '次品区', value: '次品区' },
    { label: '存货区', value: '存货区' },
])

const reset = () => {
    AreaForm.value = {
        id: '',
        name: '',
        warehouseId: '',
        category: '',
    }
    //  isPcdDisabled.value = false // 重置禁用状态
}

const save =async () => {
     if (!ruleFormRef) return
    await ruleFormRef.value.validate(async (valid, fields) => {
        if (valid) {
            let res = null
            if (AreaForm.value.id) {
                res = await updateWarehouseAreaApi(AreaForm.value)
            }else {
                res = await addWarehouseAreaApi(AreaForm.value)
            }
            if (res.code == 1) {
                ElMessage.success('操作成功')
                dialogFormVisible.value = false
                instance.emit('refresh')
            }else{
                ElMessage.error('操作失败')
            }
        } else {
        console.log('error submit!', fields)
        }
    })
}


const rules = ref({
  name: [
    { required: true, message: '名字不能为空', trigger: 'blur' }
  ],
})


const getWarehouseList = async ()=>{
  const result = await getWarehouseListApi()
  Warehouse.value = result.data
}

defineExpose({
    dialogFormVisible,
    title,
    AreaForm,
    reset,
    // isPcdDisabled // 暴露这个属性
})

onMounted(()=>{
  getWarehouseList()
})
</script>

<template>
    <el-dialog style="width: 400px;" v-model="dialogFormVisible" :title="title">
    <el-form :model="AreaForm" :rules="rules" ref="ruleFormRef" label-width="auto">
      <el-form-item label="库区名称" placeholder="请输入" prop="name" >
        <el-input v-model="AreaForm.name" />
      </el-form-item>
      <el-form-item label="所属仓库" placeholder="请输入" >
        <el-select v-model="AreaForm.warehouseId"  placeholder="请选择">
          <el-option
            v-for="item in Warehouse"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库区类型" placeholder="请输入" >
        <el-select v-model="AreaForm.category"  placeholder="请选择">
          <el-option
            v-for="item in areaType"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>